package com.industrial.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.excel.EasyExcel;

import com.industrial.app.domain.BizIndustrialElectricityConsumption;
import com.industrial.app.excel.entity.BizIndustrialElectricityConsumptionExcelEntity;
import com.industrial.app.mapper.BizIndustrialElectricityConsumptionFormalMapper;
import com.industrial.app.mapper.BizIndustrialElectricityConsumptionMapper;
import com.industrial.app.service.IBizIndustrialElectricityConsumptionService;
import com.industrial.common.core.utils.DateUtils;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.security.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 工业用电量临时(新)Service业务层处理
 *
 * @author SangGuangCan
 * @date 2024-01-22
 */
@Service
public class BizIndustrialElectricityConsumptionServiceImpl implements IBizIndustrialElectricityConsumptionService
{
    @Autowired
    private BizIndustrialElectricityConsumptionMapper bizIndustrialElectricityConsumptionMapper;

    @Autowired
    private BizIndustrialElectricityConsumptionFormalMapper bizIndustrialElectricityConsumptionFormalMapper;

    @Value("${app.file-upload-path}")
    private String fileUploadPath;

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 查询工业用电量临时(新)
     *
     * @param baseId 工业用电量临时(新)主键
     * @return 工业用电量临时(新)
     */
    @Override
    public BizIndustrialElectricityConsumption selectBizIndustrialElectricityConsumptionByBaseId(String baseId)
    {
        return bizIndustrialElectricityConsumptionMapper.selectBizIndustrialElectricityConsumptionByBaseId(baseId);
    }

    /**
     * 查询工业用电量临时(新)列表
     *
     * @param bizIndustrialElectricityConsumption 工业用电量临时(新)
     * @return 工业用电量临时(新)
     */
    @Override
    public List<BizIndustrialElectricityConsumption> selectBizIndustrialElectricityConsumptionList(BizIndustrialElectricityConsumption bizIndustrialElectricityConsumption)
    {
        if(bizIndustrialElectricityConsumption.getYear() != null){

        }
        return bizIndustrialElectricityConsumptionMapper.selectBizIndustrialElectricityConsumptionList(bizIndustrialElectricityConsumption);
    }

    /**
     * 新增工业用电量临时(新)
     *
     * @param bizIndustrialElectricityConsumption 工业用电量临时(新)
     * @return 结果
     */
    @Override
    public int insertBizIndustrialElectricityConsumption(BizIndustrialElectricityConsumption bizIndustrialElectricityConsumption)
    {
        bizIndustrialElectricityConsumption.setBaseId(IdUtil.simpleUUID());
        bizIndustrialElectricityConsumption.setBaseCreateTime(DateUtils.getNowDate());
        bizIndustrialElectricityConsumption.setBaseCreateBy(SecurityUtils.getUserId());
        return bizIndustrialElectricityConsumptionMapper.insertBizIndustrialElectricityConsumption(bizIndustrialElectricityConsumption);
    }

    /**
     * 修改工业用电量临时(新)
     *
     * @param bizIndustrialElectricityConsumption 工业用电量临时(新)
     * @return 结果
     */
    @Override
    public int updateBizIndustrialElectricityConsumption(BizIndustrialElectricityConsumption bizIndustrialElectricityConsumption)
    {
        return bizIndustrialElectricityConsumptionMapper.updateBizIndustrialElectricityConsumption(bizIndustrialElectricityConsumption);
    }

    /**
     * 批量删除工业用电量临时(新)
     *
     * @param baseIds 需要删除的工业用电量临时(新)主键
     * @return 结果
     */
    @Override
    public int deleteBizIndustrialElectricityConsumptionByBaseIds(String[] baseIds)
    {
        return bizIndustrialElectricityConsumptionMapper.deleteBizIndustrialElectricityConsumptionByBaseIds(baseIds);
    }

    /**
     * 删除工业用电量临时(新)信息
     *
     * @param baseId 工业用电量临时(新)主键
     * @return 结果
     */
    @Override
    public int deleteBizIndustrialElectricityConsumptionByBaseId(String baseId)
    {
        return bizIndustrialElectricityConsumptionMapper.deleteBizIndustrialElectricityConsumptionByBaseId(baseId);
    }

    /**
     * @param params
     * @param multipartFile
     * @return
     */
    @Override
    public AjaxResult importIndustrialElectricityConsumptionData(Map<String, String> params, MultipartFile multipartFile) {
        try{
            List<BizIndustrialElectricityConsumption> oldData = bizIndustrialElectricityConsumptionMapper.selectBizIndustrialElectricityConsumptionList(new BizIndustrialElectricityConsumption());
            if(oldData != null && !oldData.isEmpty()){
                return AjaxResult.warn("导入之前需要先清空历史数据");
            }
            /* 上传文件的路径 */
            String absoluteExcelPath = fileUploadPath + File.separator + IdUtil.simpleUUID() + ".xlsx";
            File file = new File(absoluteExcelPath);
            if(!file.exists()){
                FileUtil.touch(file);
            }
            multipartFile.transferTo(file);
//            String uploadPath = FileUploadUtils.upload(AppConfig.getProfile(),multipartFile);
//            String absoluteExcelPath = FileUploadUtils.replaceAbsolutePath(uploadPath);
            List<BizIndustrialElectricityConsumptionExcelEntity> excelDataArgs = EasyExcel.read(absoluteExcelPath).head(BizIndustrialElectricityConsumptionExcelEntity.class).headRowNumber(1).sheet().doReadSync();
            List<BizIndustrialElectricityConsumption> list = new ArrayList<>();
            for(BizIndustrialElectricityConsumptionExcelEntity item : excelDataArgs){
                BizIndustrialElectricityConsumption consumption = new BizIndustrialElectricityConsumption();
                BeanUtil.copyProperties(item,consumption,"year");
                String year = item.getYear();
                consumption.setYear(DateUtil.parse(year,"yyyy-MM"));
                consumption.setBaseCreateBy(String.valueOf(SecurityUtils.getUserId()));
                consumption.setBaseCreateTime(DateUtils.getNowDate());
                consumption.setBaseId(IdUtil.simpleUUID());
                list.add(consumption);
            }
            if(!list.isEmpty()){
                bizIndustrialElectricityConsumptionMapper.batchInsertBizIndustrialElectricityConsumption(list);
            }
        }catch (Exception e){
            return AjaxResult.warn("未知错误");
        }
        return AjaxResult.success("导入成功");
    }

    /**
     * 清空所有的工业用电量数据
     */
    @Override
    public void clearAllTableData() {
        bizIndustrialElectricityConsumptionMapper.clearAllTableData();
    }

    /**
     * <p>
     * 将临时表中的数据迁移到正式表虽
     * </p>
     *
     * @return
     */
    @Override
    public boolean transferDataInOneClick() {
        try{
            List<BizIndustrialElectricityConsumption> result = bizIndustrialElectricityConsumptionMapper.selectBizIndustrialElectricityConsumptionAllData();

            int count = 0;
            List<BizIndustrialElectricityConsumption> list = new ArrayList<>();
            for(BizIndustrialElectricityConsumption item : result){
                list.add(item);
                count++;
                if(count >= 100){
                    bizIndustrialElectricityConsumptionFormalMapper.batchInsertBizIndustrialElectricityConsumptionFormal(list);
                    list.clear();
                }
            }

            if(!list.isEmpty()){
                bizIndustrialElectricityConsumptionFormalMapper.batchInsertBizIndustrialElectricityConsumptionFormal(list);
                list.clear();
            }
            return true;
        }catch (Exception e){
            logger.error("未知错误:{}",e.getMessage());
            return false;
        }
    }
}
